package com.community.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.dto.SaleDto;
import com.community.pojo.Product;
import com.community.mapper.ProductMapper;
import com.community.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.utils.PageUtils;
import com.community.vo.ProductVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author TangHaoSong Fly ChenJie HeYuHang
 * @since 2024-05-15
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    //查询未删除的商品信息表
    @Override
    public PageUtils<Product> searchProduct(int pageNo, int pageSize, ProductVo vo) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        // 删除状态：未删除
        wrapper.eq("status", "C001-1");
        //查询条件
        if(vo != null) {
            if(!StringUtils.isEmpty(vo.getSearch_name())) {
                wrapper.like("name", vo.getSearch_name());
            }
            if(!StringUtils.isEmpty(vo.getSearch_typeId())) {
                wrapper.eq("type_id", vo.getSearch_typeId());
            }
        }
        PageHelper.startPage(vo);
        List<Product> list = this.list(wrapper);
        PageInfo<Product> pageInfo = new PageInfo<>(list);

        // PageInfo 转换成 PageUtils
        PageUtils<Product> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }

    //查询未删除的商品
    @Override
    public PageUtils<Product> queryOnOff(int pageNo, int pageSize, ProductVo vo) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        // 删除状态：未删除
        wrapper.eq("status", "C001-1");
//        //上架状态: 已上架
//        wrapper.eq("on_off","1");
        //查询条件
        if(vo != null) {
            if(!StringUtils.isEmpty(vo.getSearch_name())) {
                wrapper.like("name", vo.getSearch_name());
            }
            if(!StringUtils.isEmpty(vo.getSearch_typeId())) {
                wrapper.eq("type_id", vo.getSearch_typeId());
            }
        }

        PageHelper.startPage(pageNo, pageSize);
        List<Product> list = this.list(wrapper);
        PageInfo<Product> pageInfo = new PageInfo<>(list);

        // PageInfo 转换成 PageUtils
        PageUtils<Product> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;

    }

    @Override
    public List<SaleDto> productSale() {

        return productMapper.productSale();
    }

    @Override
    public Integer getSale() {
        return productMapper.getSale();
    }


//    @Override
//    public PageUtils<Product> searchName(ProductVo vo, int pageNo, int pageSize) {
//        QueryWrapper<Product> qw = new QueryWrapper<>();
//        if (vo != null) {
//            if (!StringUtils.isEmpty(vo.getSearch_name())) {
//                qw.eq("GATHER_ID", vo.getSearch_name());
//            }
//        }
//        PageHelper.startPage(pageNo, pageSize);
//        List<Product> list = productMapper.selectList(qw);
//        PageInfo<Product> p=new PageInfo<>(list);
//        PageUtils<Product> utils=new PageUtils<>();
//        utils.setTotal(p.getTotal());
//        utils.setRows(p.getList());
//        return utils;
//    }
}
